/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxsupplierexptapi.p                              */
/* PROCEDURE TYPE        : supplier export Process                          */
/* DESCRIPTION           : supplier export Process                          */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                27-Jul-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


 define temp-table ttSearchCondition no-undo
        field supplier_from         like Creditor.CreditorCode
        field supplier_to           like Creditor.CreditorCode
        .


 define temp-table ttsupplier no-undo

        field SharedSet_ID                like Creditor.SharedSet_ID
        field Creditor_ID                 like Creditor.Creditor_ID
        field CreditorCode                like Creditor.CreditorCode
        field CreditorIsActive            like Creditor.CreditorIsActive
        field BusinessRelation_ID         like Creditor.BusinessRelation_ID
        field CreditorType_ID             like Creditor.CreditorType_ID
        field CreditorIsTaxable           like Creditor.CreditorIsTaxable
        field PurchaseCode_ID             like Creditor.PurchaseCode_ID
        field TxuTaxUsage                 like Creditor.TxuTaxUsage
        field TxclTaxCls                  like Creditor.TxclTaxCls
        field PurchaseType_ID             like PurchaseType.PurchaseType_ID
        field PurchaseTypeCode            like PurchaseType.PurchaseTypeCode
        field PurchaseTypeDescription     like PurchaseType.PurchaseTypeDescription
        field PurchaseTypeIsActive        like PurchaseType.PurchaseTypeIsActive
        field CreditorTypeCode            like CreditorType.CreditorTypeCode
        field CreditorTypeDescription     like CreditorType.CreditorTypeDescription
        field CreditorTypeIsActive        like CreditorType.CreditorTypeIsActive
        field BusinessRelationCode        like Businessrelation.BusinessRelationCode
        field BusinessRelationName1       like Businessrelation.BusinessRelationName1
        field BusinessRelationName2       like Businessrelation.BusinessRelationName2
        field BusinessRelationName3       like Businessrelation.BusinessRelationName3
        field BusinessRelationSearchName  like Businessrelation.BusinessRelationSearchName
        field Address_ID                  like Address.Address_ID
        field AddressSearchName           like Address.AddressSearchName
        field AddressName                 like Address.AddressName
        field AddressType_ID              like Address.AddressType_ID
        field AddressStreet1              like Address.AddressStreet1
        field AddressStreet2              like Address.AddressStreet2
        field AddressStreet3              like Address.AddressStreet3
        field Country_ID                  like Address.Country_ID
        field AddressCity                 like Address.AddressCity
        field AddressState                like Address.AddressState
        field AddressTelephone            like Address.AddressTelephone
        field AddressFax                  like Address.AddressFax
        field AddressZip                  like Address.AddressZip
        field AddressEMail                like Address.AddressEMail
        field CountryCode                 like Country.CountryCode
        field CountryDescription          like Country.CountryDescription
        
          index crid Creditor_ID.
        
 define temp-table ttcontact no-undo
        field Contact_ID                  like Contact.Contact_ID
        field BusinessRelation_ID         like Contact.BusinessRelation_ID
        field Address_ID                  like Contact.Address_ID
        field CreditorCode                like Creditor.CreditorCode
        field AddressName                 like Address.AddressName
        field ContactName                 like Contact.ContactName
        field ContactEmail                like Contact.ContactEmail
        field ContactFunction             like Contact.ContactFunction
        field ContactFax                  like Contact.ContactFax
        field ContactGender               like Contact.ContactGender
        field ContactIsSecondary          like Contact.ContactIsSecondary
        field ContactIsPrimary            like Contact.ContactIsPrimary
        field ContactMobilePhone          like Contact.ContactMobilePhone
        field ContactTitle                like Contact.ContactTitle
         
      index crid CreditorCode.
      
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dsSupplier FOR ttsupplier.

   hresult = dataset dsSupplier:HANDLE.    
                

  
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "vd/"
       m_archpath    = m_archpath + "vd/"
       m_successpath = m_successpath + "vd/"
       m_errorpath   = m_errorpath + "vd/"
       m_workpath    = m_workpath + "vd/".


   empty temp-table ttsupplier.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:

   for first domains where domains.domaincode = global_domain no-lock,
         each domainsharedset of domains no-lock,
         first SharedSet of domainsharedset 
          where sharedset.sharedsettypecode = "Creditor" no-lock.
    for each ttSearchCondition.      
    if ttSearchCondition.supplier_to = "" then ttSearchCondition.supplier_to = hi_char.
     for each creditor no-lock where creditor.SharedSet_ID = SharedSet.SharedSet_ID  
                                 and creditor.creditorcode >= ttSearchCondition.supplier_from
                                 and creditor.creditorcode <= ttSearchCondition.supplier_to ,                               
         each Businessrelation of creditor no-lock,
         each address of Businessrelation no-lock,
         each addresstype of address where addresstypecode = "HEADOFFICE" /* 只获取总部地址 */,
         each country of address no-lock.
         .
        find creditortype of creditor no-lock no-error.  /* 供应商类型可能不存在 */
        find purchasetype of creditor no-lock no-error.  /* 采购类型可能不存在 */
        
        create ttsupplier.
        
        assign ttsupplier.SharedSet_ID               = Creditor.SharedSet_ID
               ttsupplier.Creditor_ID                = Creditor.Creditor_ID
               ttsupplier.CreditorCode               = Creditor.CreditorCode
               ttsupplier.CreditorIsActive           = Creditor.CreditorIsActive
               ttsupplier.BusinessRelation_ID        = Creditor.BusinessRelation_ID
               ttsupplier.CreditorType_ID            = Creditor.CreditorType_ID
               ttsupplier.CreditorIsTaxable          = Creditor.CreditorIsTaxable
               ttsupplier.PurchaseCode_ID            = Creditor.PurchaseCode_ID
               ttsupplier.TxuTaxUsage                = Creditor.TxuTaxUsage
               ttsupplier.TxclTaxCls                 = Creditor.TxclTaxCls
               ttsupplier.PurchaseType_ID            = if available purchasetype then PurchaseType.PurchaseType_ID else 0
               ttsupplier.PurchaseTypeCode           = if available purchasetype then PurchaseType.PurchaseTypeCode else ""
               ttsupplier.PurchaseTypeDescription    = if available purchasetype then PurchaseType.PurchaseTypeDescription else ""
               ttsupplier.PurchaseTypeIsActive       = if available purchasetype then PurchaseType.PurchaseTypeIsActive else false
               ttsupplier.CreditorType_ID            = if available creditortype then CreditorType.CreditorType_ID else 0
               ttsupplier.CreditorTypeCode           = if available creditortype then CreditorType.CreditorTypeCode  else ""
               ttsupplier.CreditorTypeDescription    = if available creditortype then CreditorType.CreditorTypeDescription else ""
               ttsupplier.CreditorTypeIsActive       = if available creditortype then CreditorType.CreditorTypeIsActive else false
               ttsupplier.BusinessRelationCode       = Businessrelation.BusinessRelationCode
               ttsupplier.BusinessRelationName1      = Businessrelation.BusinessRelationName1
               ttsupplier.BusinessRelationName2      = Businessrelation.BusinessRelationName2
               ttsupplier.BusinessRelationName3      = Businessrelation.BusinessRelationName3
               ttsupplier.BusinessRelationSearchName = Businessrelation.BusinessRelationSearchName
               ttsupplier.Address_ID                 = Address.Address_ID
               ttsupplier.AddressSearchName          = Address.AddressSearchName
               ttsupplier.AddressName                = Address.AddressName
               ttsupplier.AddressType_ID             = Address.AddressType_ID
               ttsupplier.AddressStreet1             = Address.AddressStreet1
               ttsupplier.AddressStreet2             = Address.AddressStreet2
               ttsupplier.AddressStreet3             = Address.AddressStreet3
               ttsupplier.Country_ID                 = Address.Country_ID
               ttsupplier.AddressCity                = Address.AddressCity
               ttsupplier.AddressState               = Address.AddressState
               ttsupplier.AddressTelephone           = Address.AddressTelephone
               ttsupplier.AddressFax                 = Address.AddressFax
               ttsupplier.AddressZip                 = Address.AddressZip
               ttsupplier.AddressEMail               = Address.AddressEMail
               ttsupplier.CountryCode                = Country.CountryCode
               ttsupplier.CountryDescription         = Country.CountryDescription
               .
               

     end. /*  for each creditor no-lock  */
     end. /* for each ttSearchCondition. */     
          
   end. /* for first domains where */
   


  end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
